<?php
				/*-------------============================================================//=*\
							  ||
				#########################################################################
				#			  || # MSTYLE NETWORK - Version 2.0							  |1|
				#	#||
				#			  || # Inc CTELECTRONICS
				------------------------------------------------------------------- #  	  
				#			  ||	Copyright © 2008-2009 Powered by Duong Viet Cuong 	  |2|
				#			  ||	All Rights Reserved
				#			  ||	 # Email : vietcuong.it@gmail.com
				@RstAkaiIto	     #  || 
				#			  ||     #			MSTYLE IS NOT FREE SOFTWARE				  
				#	# ||
				#	          || # http://mstylecode.com |
				@http://mstylecode.com/license		   									  
				#			  ||
				############################################################### 		  
							  ||														  	
				\*=============------------------------------------------------------------//-*/


class imageupload {
	
	# Config
	public $maxsize  = 1000;
	public $m_width  = 2148;
	public $m_height = 4000;
	
	public $errCode = 0 ;
	public $errString = 0 ;
	
	public $wmsource = "watermark.png";
	public $wmoverlay_opacity = 30;
	public $wmoutput_opacity = 100 ; 
	
	public $width ;
	
	public $height ;	
	
	private $fname; 
	
	public $filesize;
	
	public function imageupload($fnamex){
		$this->fname = $fnamex ;
	}
	
	# Check function	
	public function check(){
		$fname = $this->fname;
		# Check file extension		
		$extension = $this->extension($fname);
		if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))  {	
			#
			$this->errCode = 1 ;
			$this->errString = "Đuôi phải là .jpg, .jpeg, .png, hoặc .gif";
			return false;
		}
		# Check if its image file
		if (!getimagesize($_FILES[$fname]['tmp_name'])) {
			$this->errCode = 2 ;
			$this->errString = "File ảnh không chuẩn theo quy tắc";
			return false;
		}	
		
		# Get images size
		list($width, $height, $type, $attr) = getimagesize($_FILES[$fname]['tmp_name']);	
		$this->width = $width;
		$this->height = $height;
		if ($width < 1 || $height < 1 || $width > $this->m_width || $height > $this->m_height) {
			$this->errCode = 3 ;
			$this->errString = "Vui lòng kiểm tra lại ! Ảnh gửi lên phải có độ phân giải nhỏ hơn " .$this->m_width ." x ".$this->mheight;
			return false;
		}	
		
		# Check file size ( > ? Kb )
		$size = filesize($_FILES[$fname]['tmp_name']);
		if ($size > ($this->maxsize*1024) ){
			$this->errString =  "File phải có dung lượng nhỏ hơn ".$this->maxsize ."KB";		
			return false;
		}	
		$this->errCode = 0 ;
		return true ;
	}
	
	# Uploading
	public function upload($path,$ex = '') {
		$fname = $this->fname;
		chmod($path, 0777); 
		$thistime = time(); 
		$image_name= $path.'/'.$thistime.$ex.'.'.$this->extension($fname);
		$copied = @copy($_FILES[$fname]['tmp_name'],$image_name);
		chmod($path, 0644); 
		if (!$copied) {
			return false ;
		}
		else {
			return $image_name ;
		}
	}
	
	# Get extension
	function extension($fname) {
		$filename = stripslashes($_FILES[$fname]['name']); 	
		$extension = $this->getExtension($filename); 		
		$extension = strtolower($extension); 
		return $extension;
	}
	
	# Resize
	function resize($url,$nwidth,$nheight) {
		$fname = $this->fname;
		$extension = $this->extension($fname) ;
//		list($width_orig, $height_orig) = getimagesize($_FILES[$fname]['tmp_name']);
		list($width, $height, $type, $attr) = getimagesize($_FILES[$fname]['tmp_name']);	
		
		if ($width > $height) {
			# chinh theo chieu cao
			$xheight = $nheight;
			$diff = $height / $width;
			$xwidth = $nwidth / $diff;
		}
		else {
			$diff = $width / $nwidth;
			$xheight = $height / $diff;  
			$xwidth = $nwidth;
		}
		if ($nheight == 0) $nheight = $xheight ;
		chmod($path, 0777); 
		
		/*
		if ($nheight == 0) {
			# Title mode
			$nheight = $mheight ;
		}
		else {
			*/
			# Ratio mode
			# Cut is square
			/*
			if ($width > $height) $width = $height ;
			else $height = $width ;
			
			$image = imagecreatefromjpeg($url) ; 
				imagecopyresampled($tn, $image, 0, 0, 0, 0, $nwidth, $mheight, $width, $height) ; 
			
		}
	*/
	
		/*
		$ratio_orig = $width_orig/$height_orig;

		if ($nwidth/$nheight > $ratio_orig) {
			$diff = $width / $nwidth;
		    $rwidth = 
		} else {
		   $nheight = $nwidth/$ratio_orig; # Nghich ti le
		}

		$diff = $width / $nwidth;
		
		$mheight = $height / $diff;  
				if ($nheight == 0) $nheight = $mheight ;
		*/
		switch ($extension) {
			case "jpeg" :
			case "jpg" :			
				$tn = imagecreatetruecolor($nwidth, $nheight) ; 
				$image = imagecreatefromjpeg($url) ; 
				imagecopyresampled($tn, $image, 0, 0, 0, 0, $xwidth, $xheight, $width, $height) ; 
				imagejpeg($tn, $url,100) ;
			break ;
			case "png" :			
				$tn = imagecreatetruecolor($nwidth, $nheight) ; 
				$image = imagecreatefrompng($url) ; 
				imagecopyresampled($tn, $image, 0, 0, 0, 0, $nwidth, $mheight, $width, $height) ; 
				imagepng($tn, $url,100) ;
			break;
			case "gif" :			
				$tn = imagecreatetruecolor($nwidth, $nheight) ; 
				$image = imagecreatefromgif($url) ; 
				imagecopyresampled($tn, $image, 0, 0, 0, 0, $nwidth, $mheight, $width, $height) ; 
				imagegif($tn, $url,100) ;
			break;				
		}
		chmod($path, 644); 
		return true ;
	}
	
	public function remove($path,$fname) {
		if(strpos($path,'sstore') === false) {
			chmod($path, 0777); 
			if ($fh = fopen($fname, 'w')) {
				fclose($fh);
				unlink($fname);
				$this->errCode = 0 ;
			}
			else{
				$this->errCode = 6 ;
				$this->errString = "File không tồn tại";
			}
			chmod($path, 0644); 
		}
	}
	
	public function watermark( $source_file_path, $output_file_path )  {
		list( $source_width, $source_height, $source_type ) = getimagesize( $source_file_path );
 		if ( $source_type === NULL ){
   			return false;
  		}
		
		$extension = $this->getExtension($source_file_path) ;
		
		switch ( $extension ){
			case "gif":
				$source_gd_image = imagecreatefromgif( $source_file_path );
				break;
			case "jpg":	
			case "jpeg":
				$source_gd_image = imagecreatefromjpeg( $source_file_path );
				break;
			case "png":
				$source_gd_image = imagecreatefrompng( $source_file_path );
				break;
		default:
			return false;
		}
		
		$overlay_gd_image = imagecreatefrompng( $this->wmsource );
		$overlay_width = imagesx( $overlay_gd_image );
		$overlay_height = imagesy( $overlay_gd_image );
			
			 //--------------------------------
			 // ALIGN BOTTOM, LEFT
			 //--------------------------------
			
			 imagecopymerge(
			  $source_gd_image,
			  $overlay_gd_image,
			  0,
			  $source_height - $overlay_height,
			  0,
			  0,
			  $overlay_width,
			  $overlay_height,
			  $this->wmoverlay_opacity
			 );

		imagejpeg( $source_gd_image, $output_file_path, $this->wmoutput_opacity );
		imagedestroy( $source_gd_image );
		imagedestroy( $overlay_gd_image );
	}
	
	function getextension($str){
		$i = strrpos($str,".");
	if (!$i) {
		return "";
	}
	$l = strlen($str) - $i;
	$ext = substr($str,$i+1,$l); 
	return strtolower($ext);
	}
	
	
	
	
	
	
	
	
	
}


//$upload->m_width = 900;
//$upload->test();


?>